Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove defensive Task[] copy from non-generic Task.WhenAll #81065

Merged
merged 1 commit into from
Jan 26, 2023

Conversation

stephentoub
Copy link
Member

When calling it with a Task[] or List<Task>, we'll no longer generate a temporary array; the only allocation in those cases will be for the returned Task itself, at least if it completes successfully (if multiple of the constituent tasks fail or are canceled, we'll end up with an additional list allocation beyond what we had before). If you call it with an enumerable that's not an ICollection<Task>, we'll also save a Task[].

Contributes to #77873

Method Toolchain Mean Ratio Allocated Alloc Ratio
WhenAll_Array \main\corerun.exe 155.5 ns 1.00 304 B 1.00
WhenAll_Array \pr\corerun.exe 149.6 ns 0.96 264 B 0.87
WhenAll_List \main\corerun.exe 212.3 ns 1.00 376 B 1.00
WhenAll_List \pr\corerun.exe 178.5 ns 0.87 296 B 0.79
WhenAll_Collection \main\corerun.exe 210.3 ns 1.00 360 B 1.00
WhenAll_Collection \pr\corerun.exe 177.6 ns 0.82 328 B 0.91
WhenAll_Enumerable \main\corerun.exe 241.8 ns 1.00 472 B 1.00
WhenAll_Enumerable \pr\corerun.exe 239.8 ns 0.99 432 B 0.92
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

[MemoryDiagnoser(false)]
public partial class Program
{
    static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

    [Benchmark]
    public void WhenAll_Array()
    {
        AsyncTaskMethodBuilder atmb1 = AsyncTaskMethodBuilder.Create();
        AsyncTaskMethodBuilder atmb2 = AsyncTaskMethodBuilder.Create();
        Task whenAll = Task.WhenAll(atmb1.Task, atmb2.Task);
        atmb1.SetResult();
        atmb2.SetResult();
        whenAll.Wait();
    }

    [Benchmark]
    public void WhenAll_List()
    {
        AsyncTaskMethodBuilder atmb1 = AsyncTaskMethodBuilder.Create();
        AsyncTaskMethodBuilder atmb2 = AsyncTaskMethodBuilder.Create();
        Task whenAll = Task.WhenAll(new List<Task>(2) { atmb1.Task, atmb2.Task });
        atmb1.SetResult();
        atmb2.SetResult();
        whenAll.Wait();
    }

    [Benchmark]
    public void WhenAll_Collection()
    {
        AsyncTaskMethodBuilder atmb1 = AsyncTaskMethodBuilder.Create();
        AsyncTaskMethodBuilder atmb2 = AsyncTaskMethodBuilder.Create();
        Task whenAll = Task.WhenAll(new ReadOnlyCollection<Task>(new[] { atmb1.Task, atmb2.Task }));
        atmb1.SetResult();
        atmb2.SetResult();
        whenAll.Wait();
    }

    [Benchmark]
    public void WhenAll_Enumerable()
    {
        AsyncTaskMethodBuilder atmb1 = AsyncTaskMethodBuilder.Create();
        AsyncTaskMethodBuilder atmb2 = AsyncTaskMethodBuilder.Create();
        var q = new Queue<Task>(2);
        q.Enqueue(atmb1.Task);
        q.Enqueue(atmb2.Task);
        Task whenAll = Task.WhenAll(q);
        atmb1.SetResult();
        atmb2.SetResult();
        whenAll.Wait();
    }
}

When calling it with a `Task[]` or `List<Task>`, we'll no longer generate a temporary array; the only allocation in those cases will be for the returned Task itself, at least if it completes successfully (if multiple of the constituent tasks fail or are canceled, we'll end up with an additional list allocation beyond what we had before).  If you call it with an enumerable that's not an `ICollection<Task>`, we'll also save a `Task[]`.
@stephentoub stephentoub added this to the 8.0.0 milestone Jan 24, 2023
@stephentoub stephentoub added the tenet-performance Performance related issue label Jan 24, 2023
@ghost ghost assigned stephentoub Jan 24, 2023
@ghost
Copy link

ghost commented Jan 24, 2023

Tagging subscribers to this area: @dotnet/area-system-threading-tasks
See info in area-owners.md if you want to be subscribed.

Issue Details

When calling it with a Task[] or List<Task>, we'll no longer generate a temporary array; the only allocation in those cases will be for the returned Task itself, at least if it completes successfully (if multiple of the constituent tasks fail or are canceled, we'll end up with an additional list allocation beyond what we had before). If you call it with an enumerable that's not an ICollection<Task>, we'll also save a Task[].

Contributes to #77873

Method Toolchain Mean Ratio Allocated Alloc Ratio
WhenAll_Array \main\corerun.exe 155.5 ns 1.00 304 B 1.00
WhenAll_Array \pr\corerun.exe 149.6 ns 0.96 264 B 0.87
WhenAll_List \main\corerun.exe 212.3 ns 1.00 376 B 1.00
WhenAll_List \pr\corerun.exe 178.5 ns 0.87 296 B 0.79
WhenAll_Collection \main\corerun.exe 210.3 ns 1.00 360 B 1.00
WhenAll_Collection \pr\corerun.exe 177.6 ns 0.82 328 B 0.91
WhenAll_Enumerable \main\corerun.exe 241.8 ns 1.00 472 B 1.00
WhenAll_Enumerable \pr\corerun.exe 239.8 ns 0.99 432 B 0.92
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

[MemoryDiagnoser(false)]
public partial class Program
{
    static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

    [Benchmark]
    public void WhenAll_Array()
    {
        AsyncTaskMethodBuilder atmb1 = AsyncTaskMethodBuilder.Create();
        AsyncTaskMethodBuilder atmb2 = AsyncTaskMethodBuilder.Create();
        Task whenAll = Task.WhenAll(atmb1.Task, atmb2.Task);
        atmb1.SetResult();
        atmb2.SetResult();
        whenAll.Wait();
    }

    [Benchmark]
    public void WhenAll_List()
    {
        AsyncTaskMethodBuilder atmb1 = AsyncTaskMethodBuilder.Create();
        AsyncTaskMethodBuilder atmb2 = AsyncTaskMethodBuilder.Create();
        Task whenAll = Task.WhenAll(new List<Task>(2) { atmb1.Task, atmb2.Task });
        atmb1.SetResult();
        atmb2.SetResult();
        whenAll.Wait();
    }

    [Benchmark]
    public void WhenAll_Collection()
    {
        AsyncTaskMethodBuilder atmb1 = AsyncTaskMethodBuilder.Create();
        AsyncTaskMethodBuilder atmb2 = AsyncTaskMethodBuilder.Create();
        Task whenAll = Task.WhenAll(new ReadOnlyCollection<Task>(new[] { atmb1.Task, atmb2.Task }));
        atmb1.SetResult();
        atmb2.SetResult();
        whenAll.Wait();
    }

    [Benchmark]
    public void WhenAll_Enumerable()
    {
        AsyncTaskMethodBuilder atmb1 = AsyncTaskMethodBuilder.Create();
        AsyncTaskMethodBuilder atmb2 = AsyncTaskMethodBuilder.Create();
        var q = new Queue<Task>(2);
        q.Enqueue(atmb1.Task);
        q.Enqueue(atmb2.Task);
        Task whenAll = Task.WhenAll(q);
        atmb1.SetResult();
        atmb2.SetResult();
        whenAll.Wait();
    }
}
Author: stephentoub
Assignees: -
Labels:

area-System.Threading.Tasks

Milestone: -

Copy link
Contributor

@dakersnar dakersnar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM at a high level.

@stephentoub stephentoub merged commit 8ba2ad4 into dotnet:main Jan 26, 2023
@stephentoub stephentoub deleted the taskwhenalldefensive branch January 26, 2023 23:31
@ghost ghost locked as resolved and limited conversation to collaborators Feb 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants